/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#ifndef __PQ_PlayerSessionManager_H__
#define __PQ_PlayerSessionManager_H__

#include "PQNet.h"
#include "TCPSocket.h"
#include "PlayerSession.h"

namespace PQServiceLogin
{
	/*This class manage a group of players' session
	*/
	class PlayerSessionManager
	{
	public:
		PlayerSessionManager(void);
		~PlayerSessionManager(void);

		inline int getPlayerCount()
		{
			return _sessionCount;
		}

		void init();
		void select();
		void update();

		bool addSession(PlayerSession* session);
		void removeSession(PlayerSession* session);

	protected:
		//process network exceptions
		void	processExceptions();

		bool processInput();
		bool processOutput();
		bool processCommand();

		bool acceptConnection();
	public:
		enum{
			SELECT_BAK = 0,
			SELECT_USE = 1,
			SELECT_MAX = 2, 
		};
	protected://network related
		PQNet::TCPSocket* _listenSocket;
		
		fd_set _fdRead[SELECT_MAX];
		fd_set _fdWrite[SELECT_MAX];
		fd_set _fdExcept[SELECT_MAX];
		SOCKET _fdMax;
		SOCKET _fdMin;

		timeval _timeout[SELECT_MAX];

	protected:
		PlayerSession* _playerSessions[MAX_PLAYER];
		int _sessionCount;
	};
}
#endif
